<template>
	<div class="system-role-container">
		<el-card shadow="hover">
			<div class="system-user-search mb15">
				<el-form :inline="true">
					<!-- <el-form-item label="角色名称">
            <el-input size="default" v-model="tableData.param.roleName" placeholder="请输入角色名称" class="w-50 m-2" clearable/>
          </el-form-item> -->
					<el-form-item label="ID">
						<el-input v-model="tableData.queryParams.titleid" :placeholder="'请输入ID'" clearable class="w-50 m-2"
							size="default" @input="keywordChangeid($event)" />
					</el-form-item>
					<Sousuo v-on:customEvent="handleCustomEvent"> </Sousuo>

					<el-form-item label="平台">
						<el-input v-model="tableData.queryParams.titlept" :placeholder="'请输入平台名'" clearable class="w-50 m-2"
							size="default" @input="keywordChangept($event)" />
					</el-form-item>
					<el-form-item>
						<el-button size="default" type="primary" class="ml10" @click="apiroleList">
							<el-icon>
								<ele-Search />
							</el-icon>
							查询
						</el-button>
						<el-button size="default" type="success" class="ml10" @click="onOpenAddRole">
							<el-icon>
								<ele-FolderAdd />
							</el-icon>
							新增会员等级
						</el-button>
						<el-button size="default" type="primary" class="ml10" @click="Refresh">
							<!-- <el-icon><Refresh /></el-icon> -->
							刷新页面
						</el-button>
					</el-form-item>
				</el-form>
			</div>
			<el-table :data="tableData.data" style="width: 100%">
				<!-- <el-table-column type="index" label="ID
" width="60" /> -->
				<el-table-column fixed prop="id" label="ID" show-overflow-tooltip></el-table-column>
				<el-table-column prop="vipName" label="等级名称" width="100"></el-table-column>
				<el-table-column prop="recharge" label="充值累计" show-overflow-tooltip></el-table-column>
				<!-- <el-table-column prop="status" label="等级" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.status===1">启用</el-tag>
						<el-tag type="info" v-else>禁用</el-tag>
					</template>
				</el-table-column> -->
				<el-table-column prop="flow" label="流水累计" show-overflow-tooltip></el-table-column>
				<el-table-column prop="r1" label="比例1" show-overflow-tooltip></el-table-column>
				<el-table-column prop="r2" label="比例2" show-overflow-tooltip></el-table-column>
				<el-table-column prop="r3" label="比例3" show-overflow-tooltip></el-table-column>
				<el-table-column prop="r4" label="比例4" show-overflow-tooltip></el-table-column>
				<el-table-column prop="r5" label="比例5" show-overflow-tooltip></el-table-column>
				<el-table-column prop="r6" label="比例6" show-overflow-tooltip></el-table-column>
				<el-table-column prop="r7" label="比例7" show-overflow-tooltip></el-table-column>
				<el-table-column prop="status" label="状态" v-slot="{ row }">{{ row.status === 0 ? '正常' : '禁用' }}</el-table-column>
				<!-- <el-table-column prop="Game_balance" label="对应图片" show-overflow-tooltip></el-table-column> -->

				<el-table-column prop="updatedAt" label="创建时间" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" width="300">
					<template #default="scope">
						<el-button size="small" text type="primary"
							@click="onOpenEditRole(scope.row)"><el-icon><ele-EditPen /></el-icon>修改</el-button>
						<el-button size="small" text type="primary"
							@click="onRowDel(scope.row)"><el-icon><ele-DeleteFilled /></el-icon>删除</el-button>
						<!-- <el-button size="small" text type="primary" @click="Adjusting_Balanceaon"><el-icon><ele-EditPen /></el-icon>显示</el-button> -->
					</template>
				</el-table-column>
			</el-table>
			<pagination v-show="tableData.total > 0" :total="tableData.total" v-model:page="tableData.param.pageNum"
				v-model:limit="tableData.param.pageSize" @pagination="apiroleList" />
		</el-card>
		<!-- <EditRole ref="editRoleRef" @getRoleList="roleList"/> -->
		<MemberListadd ref="editRoleRef" @getRoleList="apiroleList" :pd="tableData.pdcs" :edit="tableData.data" />
		<Adjusting_Balance ref="Adjusting" @getRoleList="apiroleList" />

	</div>
</template>

<script lang="ts">
import { toRefs, reactive, onMounted, ref, defineComponent, toRaw, getCurrentInstance } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import MemberListadd from './component/berListadd.vue';
import Adjusting_Balance from './component/Adjusting_balance.vue';
import Sousuo from '/@/components/sousuo/index.vue';
import { apilevelList, apideletelevelList } from '/@/api/MemberManagement/MemberListlevel';

// 定义接口来定义对象的类型
interface TableData {
	id: number;
	vipName: string;
	vipType: number;
	recharge: number;
	flow: number;
	r1: number;
	r2: number;
	r3: number;
	r4: number;
	r5: number;
	r6: number;
	r7: number;
	exp: number;
	status: string;
	isDefault: number;
	createdAt: string;
	updatedAt: string;
	vipPic: string;
	vrBerFee: number;
	ldFee: number;
	level: number;
	// queryParams:Array<TableData>;
}

interface TableDataState {
	tableData: {
		queryParams: {
			titleid: string;
			titlename: string;
			titlept: string;
			component: string;
		};
		sourceData: Array<sourceDataff>;
		data: Array<TableData>;
		total: number;
		loading: boolean;
		param: {
			pageNum: number;
			pageSize: number;
		};
		pdcs: number;
	};
}
interface sourceDataff {
	value: string;
	label: string;
}

export default defineComponent({
	name: 'apiV1SystemRoleList',
	components: { MemberListadd, Adjusting_Balance, Sousuo },
	//总方法
	setup() {
		// const { proxy } = getCurrentInstance() as any;
		const addRoleRef = ref();
		const editRoleRef = ref();
		const Adjusting = ref();
		const dataScopeRef = ref();
		const value = ref();
		const state = reactive<TableDataState>({
			tableData: {
				queryParams: {
					title: '',
					component: '',
				},
				data: [],
				sourceData: [
					{ value: '0', label: 'ID' },
					{ value: '1', label: '用户名' },
					{ value: '2', label: '平台' },
				],
				total: 0,
				loading: false,
				param: {
					roleName: '',
					roleStatus: '',
					pageNum: 1,
					pageSize: 10,
				},
				pdcs: 0,
			},
		});
		const data = ref(state.tableData.sourceData);

		// 初始化表格数据
		const apiinitTableData = () => {
			apiroleList();
		};
		// 初始化表格数据
		const apiroleList = () => {

			apilevelList(state.tableData.param).then((res) => {

				const list = res.data.list ?? [];
				const data: Array<TableData> = [];
				list.map((item: TableData) => {

					data.push({
						id: item.id,
						vipName: item.vipName,
						vipType: item.vipType,
						recharge: item.recharge,
						flow: item.flow,
						r1: item.r1,
						r2: item.r2,
						r3: item.r3,
						r4: item.r4,
						r5: item.r5,
						r6: item.r6,
						r7: item.r7,
						exp: item.exp,
						status: item.status,
						isDefault: item.isDefault,
						createdAt: item.createdAt,
						updatedAt: item.updatedAt,
						vipPic: item.vipPic,
						vrBerFee: item.vrBerFee,
						ldFee: item.ldFee,
						level: item.level
					});
				});
				state.tableData.data = data;

				// state.tableData.total = res.data.total;
			});
		};
		const handleCustomEvent = (data: any) => {
			apiroleLista(data);

		};
		// 搜索
		const apiroleLista = (id: any) => {
			state.tableData.param.id = id
			apilevelList(state.tableData.param).then((res) => {
				const list = res.data.list ?? [];
				const data: Array<TableData> = [];
				list.map((item: TableData) => {
					data.push({
						id: item.id,
						vipName: item.vipName,
						vipType: item.vipType,
						recharge: item.recharge,
						flow: item.flow,
						r1: item.r1,
						r2: item.r2,
						r3: item.r3,
						r4: item.r4,
						r5: item.r5,
						r6: item.r6,
						r7: item.r7,
						exp: item.exp,
						status: item.status,
						isDefault: item.isDefault,
						createdAt: item.createdAt,
						updatedAt: item.updatedAt,
						vipPic: item.vipPic,
						vrBerFee: item.vrBerFee,
						ldFee: item.ldFee,
						level: item.level
					});
				});
				state.tableData.data = data;
			});
		};
		//文本框搜索
		const keywordChangeid = (keywords: any) => {
			apiroleLista(keywords);

		};
		//文本框搜索
		const keywordChangename = (keywords: any) => {
			apiroleLista(keywords);

		};
		//文本框搜索
		const keywordChangept = (keywords: any) => { 
			apiroleLista(keywords);

		};
		// 筛选
		const filterable = (value: any) => {
		};
		// 打开新增角色弹窗
		const onOpenAddRole = () => {
			state.tableData.pdcs = 0;
			editRoleRef.value.openDialog();
		};
		// 打开修改角色弹窗
		const onOpenEditRole = (row: Object) => {
			// console.log(row)
			state.tableData.pdcs = 1;
			editRoleRef.value.openDialog(toRaw(row));
		};
		// 调整余额
		const Adjusting_Balanceaon = () => {
			Adjusting.value.openDialoga();
		};
		// 删除角色
		const onRowDel = (row: any) => {
			ElMessageBox.confirm(`此操作将永久删除此会员等级：“${row.vipName}”，是否继续?`, '提示', {
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(() => {
					apideletelevelList(row.id).then(() => {
						ElMessage.success('删除成功');
						// proxy.$refs['editRoleRef'].resetMenuSession();
						apiroleList();
					});
				})
				.catch(() => { });
		};
		// 刷新页面
		const Refresh = () => {
			apiinitTableData(); //页面进入渲染
		};

		// 分页改变
		const onHandleSizeChange = (val: number) => {
			state.tableData.param.pageSize = val;
		};
		// 分页改变
		const onHandleCurrentChange = (val: number) => {
			state.tableData.param.pageNum = val;
		};
		// 页面加载时
		onMounted(() => {
			apiinitTableData(); //页面进入渲染
		});

		return {
			handleCustomEvent,
			value,
			Adjusting,
			addRoleRef,
			editRoleRef,
			dataScopeRef,
			onOpenAddRole,
			onOpenEditRole,
			onRowDel,
			onHandleSizeChange,
			onHandleCurrentChange,
			apiroleList,
			Refresh,
			Adjusting_Balanceaon,
			...toRefs(state),
			keywordChangeid,
			keywordChangename,
			keywordChangept,
			filterable,
			data,
		};
	},
});
</script>
